﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Data;
using Microsoft.Forums.OfflineStore;
using System.Windows.Threading;
using System.ComponentModel;

namespace Microsoft.Forums.ForumsClient.Models
{
    /// <summary>
    /// Data Model for Options Dialog
    /// </summary>
    public class OptionsDataModel : BindableObject
    {
        private bool isHeaderOnlyModeSet;
        private bool isAutoSynchModeSet;
        private int synchFrequencyIndex;
        private int postAutoExpireAgeIndex;

        /// <summary>
        /// Constructor of Data Model for Synch Settings Dialog
        /// </summary>
        public OptionsDataModel()
        {
            SyncFrequencyCollection = new DispatchingObservableCollection<string> { "Every 5 minutes", "Every 10 minutes", "Every 15 minutes", "Every 30 minutes", "Every 60 minutes", "Once Per Day" };
            SyncFrequencyCollectionView = new ListCollectionView(SyncFrequencyCollection);

            PostAutoExpireAgeCollection = new DispatchingObservableCollection<string> { "Never", "7 Days", "30 Days", "60 Days", "90 Days" };
            PostAutoExpireAgeCollectionView = new ListCollectionView(PostAutoExpireAgeCollection);

            IsHeaderOnlyModeSet = Properties.Settings.Default.IsHeaderOnlyModeOn;
            IsAutoSynchModeSet = Properties.Settings.Default.IsAutoSyncOn;

            switch(Properties.Settings.Default.SyncFrequency)
            {
                case 300:
                    // 5 minutes in seconds so index is 0
                    synchFrequencyIndex = 0;
                    break;
                case 600:
                    // 10 minutes in seconds so index is 1
                    synchFrequencyIndex = 1;
                    break;
                case 900:
                    // 15 minutes in seconds so index is 2
                    synchFrequencyIndex = 2;
                    break;
                case 1800:
                    // 30 minutes in seconds so index is 3
                    synchFrequencyIndex = 3;
                    break;
                case 3600:
                    // 60 minutes in seconds so index is 4
                    synchFrequencyIndex = 4;
                    break;
                case 122400:
                    // once a day in seconds so index is 5
                    synchFrequencyIndex = 5;
                    break;
                default:
                    synchFrequencyIndex = -1;
                    break;
            }

            if (synchFrequencyIndex > -1)
            {
                SyncFrequencyCollectionView.MoveCurrentTo(SyncFrequencyCollection[synchFrequencyIndex]);
            }

            switch (Properties.Settings.Default.AutoExpireAge)
            {
                case -1:
                    // User specified Never so index is 0
                    postAutoExpireAgeIndex = 0;
                    break;
                case 7:
                    // User specified 7 days so index is 1
                    postAutoExpireAgeIndex = 1;
                    break;
                case 30:
                    // User specified 30 days so index is 2
                    postAutoExpireAgeIndex = 2;
                    break;
                case 60:
                    // User specified 60 days so index is 3
                    postAutoExpireAgeIndex = 3;
                    break;
                case 90:
                    // User specified 90 days so index is 4
                    postAutoExpireAgeIndex = 4;
                    break;
                default:
                    postAutoExpireAgeIndex = -1;
                    break;
            }

            if (postAutoExpireAgeIndex > -1)
            {
                PostAutoExpireAgeCollectionView.MoveCurrentTo(PostAutoExpireAgeCollection[postAutoExpireAgeIndex]);
            }
        }

        /// <summary>
        /// gets/sets synch frequency combo box item from the collection 
        /// </summary>
        public ObservableCollection<string> SyncFrequencyCollection
        {
            get;
            private set;
        }

        /// <summary>
        /// gets/sets synch frequency combo box item from the view
        /// </summary>
        public ListCollectionView SyncFrequencyCollectionView
        {
            get;
            private set;
        }

        /// <summary>
        /// gets/sets auto expiration of posts combo box item from the collection 
        /// </summary>
        public ObservableCollection<string> PostAutoExpireAgeCollection
        {
            get;
            private set;
        }

        /// <summary>
        /// gets/sets auto expiration of posts combo box item from the view 
        /// </summary>
        public ListCollectionView PostAutoExpireAgeCollectionView
        {
            get;
            private set;
        }


        /// <summary>
        /// gets/sets header only mode value
        /// </summary>
        public bool IsHeaderOnlyModeSet
        {
            get { return isHeaderOnlyModeSet; }
            set
            {
                if (value != isHeaderOnlyModeSet)
                {
                    isHeaderOnlyModeSet = value;
                    OnPropertyChanged("IsHeaderOnlyModeSet");
                }
            }
        }

        /// <summary>
        /// gets/sets auto synch enabled setting
        /// </summary>
        public bool IsAutoSynchModeSet
        {
            get { return isAutoSynchModeSet; }
            set
            {
                if (value != isAutoSynchModeSet)
                {
                    isAutoSynchModeSet = value;
                    OnPropertyChanged("IsAutoSynchModeSet");
                }
            }
        }

    }
}
